package com.washmadam.service.service.order.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.washmadam.service.common.enums.DeleteEnum;
import com.washmadam.service.common.enums.RenewalStatusEnum;
import com.washmadam.service.common.enums.ReserveStatusEnum;
import com.washmadam.service.common.enums.StatusEnum;
import com.washmadam.service.db.dao.CustomerOrderMapper;
import com.washmadam.service.db.dto.CustomerOrder;
import com.washmadam.service.db.example.CustomerOrderExample;
import com.washmadam.service.db.request.operate.CustomerOrderOperateParam;
import com.washmadam.service.service.order.CustomerOrderOperateService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class CustomerOrderOperateServiceImpl implements CustomerOrderOperateService {
	@Autowired
	private CustomerOrderMapper customerOrderMapper;

	@Override
	public void saveOrUpdateOrder(CustomerOrderOperateParam requestParam) {
		if (requestParam.getId() == null) {
			customerOrderMapper.insertSelective(requestParam);
			return;
		}
		customerOrderMapper.updateByPrimaryKeySelective(requestParam);
	}

	@Override
	public void destroyOldCustomerOrder(CustomerOrderOperateParam requestParam) {
		CustomerOrderExample example = new CustomerOrderExample();
		example.createCriteria()
		       .andReserveStatusEqualTo(ReserveStatusEnum.MEMBER_END.getCode())
		       .andStatusEqualTo(StatusEnum.ENABLE.getCode())
		       .andIsDeleteEqualTo(DeleteEnum.NORMAL.getCode());
		CustomerOrder record = new CustomerOrder();
		record.setStatus(StatusEnum.PROHIBIT.getCode());
		record.setUpdateBy(requestParam.getUpdateBy());
		record.setUpdateTime(new Date());
		customerOrderMapper.updateByExampleSelective(record, example);
	}
	
	@Override
	public void renewalOldCustomerOrderById(CustomerOrderOperateParam requestParam) {
		if(requestParam.getOriginOrderId() == null) {
			return;
		}
		CustomerOrder record = new CustomerOrder();
		record.setId(requestParam.getOriginOrderId());
		record.setReserveStatus(ReserveStatusEnum.MEMBER_END.getCode());
		record.setRenewalStatus(RenewalStatusEnum.RENEWAL.getCode());
		record.setStatus(StatusEnum.ENABLE.getCode());
		record.setUpdateBy(requestParam.getUpdateBy());
		record.setUpdateTime(new Date());
		customerOrderMapper.updateByPrimaryKeySelective(record);
	}

}
